﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wfTree
{
	public partial class ctlPerson : UserControl
	{
		public DbFtPerson Person = null;
		public DbFtPerson Spouse = null;

		private bool _entered = false;
		//private bool _enteredSpouse = false;

		public ctlPerson()
		{
			InitializeComponent();
		}

		public void UpdatePerson()
		{
			clearData();
			loadData();
		}

		public void UpdatePerson(DbFtPerson person, DbFtPerson spouse=null)
		{
			clearData();
			Person = person;
			Spouse = spouse;
			loadData();
		}

		private void lbSpouse_Click(object sender, EventArgs e)
		{
			pnlSpouse_MouseClick(sender, null);
		}
		private void pnlSpouse_MouseClick(object sender, MouseEventArgs e)
		{
			DbFtPerson pt = Person;
			Person = Spouse;
			Spouse = pt;
			//if (Person.PersonId != GlobalData.PersonInFocus.PersonId)
			Program.MainForm.LoadUpPerson(Person.PersonId);

			ctlPerson_Load(sender, e);
		}

		private void pnlPerson_MouseClick(object sender, MouseEventArgs e)
		{
			// this click should be passed back through to the parent form?
			Program.MainForm.LoadUpPerson(Person.PersonId, true);
		}

		private void ctlPerson_Load(object sender, EventArgs e)
		{
			pbEdit.Visible = false;
			pbRemove.Visible = false;
			pbRemoveSpouse.Visible = false;
			loadData();
		}

		private void clearData()
		{

		}

		private void loadData()
		{
			if (Person == null)
			{
				this.Visible = false;
				return;
			}
			this.Visible = true;

			lbFullName.Text = Person.ShortName;

			if (Spouse == null)
			{
				if (Person.SpousePersonId > 0)
				{
					Spouse = new DbFtPerson();
					Spouse.PersonId = Person.SpousePersonId;
					if (!Spouse.Select())
						Spouse = null;
				}
			}

			if (Spouse == null || Spouse.PersonId == 0)
			{
				pnlSpouse.Visible = false;
				//btnAddSpouse.Visible = true;
			}
			else
			{
				pnlSpouse.Visible = true;
				//btnAddSpouse.Visible = false;
				lbSpouse.Text = Spouse.ShortName;
			}
		}

		private void btnAddSpouse_Click(object sender, EventArgs e)
		{
		}

		private void btEdit_Click(object sender, EventArgs e)
		{

		}

		private void pbAddSpouse_Click(object sender, EventArgs e)
		{
			Program.FUserDetail.Relation = Relationship.Spouse;
			Program.FUserDetail.PersonRelatedTo = Person;
			Program.FUserDetail.ShowDialog(this.ParentForm);
			if (Program.FUserDetail.PersonEdit != null)
				ctlPerson_Load(sender, e);
		}

		private void lbAddSpouse_Click(object sender, EventArgs e)
		{
			pbAddSpouse_Click(sender, e);
		}

		private void ctlPerson_MouseEnter(object sender, EventArgs e)
		{
			_entered = true;
			pbEdit.Visible = true;
			//pbRemove.Visible = true;
		}

		private void ctlPerson_MouseLeave(object sender, EventArgs e)
		{
			_entered = false;
			timer1.Enabled = true;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (_entered == false)
			{
				pbEdit.Visible = false;
				//pbRemove.Visible = false;
			}
			//if (_enteredSpouse == false)
			//	pbRemoveSpouse.Visible = false;
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			Program.FUserDetail.Relation = Relationship.Primary;
			Program.FUserDetail.PersonEdit = Person;
			Program.FUserDetail.ShowDialog(this.ParentForm);
			if (Program.FUserDetail.PersonEdit != null)
				ctlPerson_Load(sender, e);
		}

		private void pbRemoveSpouse_MouseEnter(object sender, EventArgs e)
		{
			//_enteredSpouse = true;
			//pbRemoveSpouse.Visible = true;
		}

		private void pbRemoveSpouse_MouseLeave(object sender, EventArgs e)
		{
			//_enteredSpouse = false;
			//timer1.Enabled = true;
		}
	}
}
